package com.lzh.shijicloudstack.security;


import com.alibaba.fastjson.JSON;
import com.lzh.shijicloudstack.common.R;
import com.lzh.shijicloudstack.config.enums.request.ErrorCodeEnum;
import com.lzh.shijicloudstack.common.ResultUtil;
import com.lzh.shijicloudstack.utils.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author luzhiheng
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        R r = ResultUtil.error(ErrorCodeEnum.NOT_LOGIN_ERROR);
        String json = JSON.toJSONString(r);
        WebUtils.renderString(response, json);
    }
}
